package com.scwatch.core.web.back;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.*;
import com.scwatch.core.service.CommentService;
import com.scwatch.core.service.InfoDetailService;
import com.scwatch.core.service.InfoQueryService;
import com.scwatch.core.service.InfoService;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/core/info_detail")
public class InfoDetailController {
    private static final Logger logger = LoggerFactory.getLogger(MemberGroupController.class);

    @RequiresPermissions("core:info_detail:comment_Info")
    @RequestMapping("comment_Info.do")
    public String commentInfo(HttpServletRequest request, org.springframework.ui.Model modelMap, Integer id, @PageableDefault(sort = {"publishDate", "id"}, direction = Sort.Direction.DESC) Pageable pageable) {
        Integer siteId = Context.getCurrentSiteId();
        User user = Context.getCurrentUser();
        Integer nodeId = null;
        Integer mainNodeId = null;
        String treeNumber = null;
        Node queryNode = null;

        boolean allInfoPerm = user.getAllInfoPerm(siteId);

        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);
        int infoPermType = user.getInfoPermType(siteId);
        infoPermType = 0;

        Page<Info> pagedList = infoQueryService.findAll(null,siteId, null, null, "000", user.getId(), allInfoPerm,
                infoPermType, null, params, pageable, null);
//        List<Attribute> attributeList = attributeService.findList(siteId);
//        pagedList.getContent().get(0).getDetail()
        List<Info> listnew = new ArrayList<Info>();
       List<Info> list = pagedList.getContent();
        for (Info info : list){
           Integer Ids =  info.getId();
           try {
               Long totalCont = commentService.findTotalCont("Info",Ids);
               info.setTotalCont(totalCont);
           }finally {
               try {
                   long zcount = commentService.findZCont("Info",Ids,0);
                   info.setZcont(zcount);
               }finally {
                   listnew.add(info);
               }
           }
        }
        modelMap.addAttribute("pagedList", pagedList);
        modelMap.addAttribute("listnew", listnew);
//        modelMap.addAttribute("attributeList", attributeList);
//        modelMap.addAttribute("queryNodeId", queryNodeId);
//        modelMap.addAttribute("queryNodeType", queryNodeType);
//        modelMap.addAttribute("queryInfoPermType", queryInfoPermType);
//        modelMap.addAttribute("queryStatus", queryStatus);

//        List<Info> infos = serviceinfo.

//                modelMap.addAttribute("infoList", infoList);
        return "core/info_detail/comment_info_list";
    }

    @Autowired
    private InfoDetailService service;

    @Autowired
    private InfoService serviceinfo;
    @Autowired
    private CommentService commentService;

    @Autowired
    private InfoQueryService infoQueryService;
}
